package com.snail.readyvr.activity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.di.component.DaggerMeActivityPresenterComponent;
import com.snail.readyvr.di.module.MeActivityPresenterModule;
import com.snail.readyvr.dialog.SelectHeadIconDialog;
import com.snail.readyvr.interfaces.OnClickOneEdit;
import com.snail.readyvr.mvp.contract.MeActivityContract;
import com.snail.readyvr.mvp.model.ChangeHeadIconParam;
import com.snail.readyvr.mvp.model.ChangeNickNameParam;
import com.snail.readyvr.mvp.presenter.MeActivityPresenter;
import com.snail.readyvr.ui.RoundedImageView;
import com.snail.readyvr.util.DialogUtils;
import com.snail.readyvr.util.IntentParams;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.StringUtils;
import com.snail.readyvr.util.ToastUtils;
import com.snail.readyvr.util.Utils;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;


/**
 * Created by MRKING on 2016/10/14.
 */

public class MeActivity extends CheckPermissionsActivity implements MeActivityContract.View {
    @BindView(R.id.change_psw_tip)
    TextView mChangePswTip;
    @BindView(R.id.my_account)
    TextView mMyAccount;
    @BindView(R.id.my_email_phone)
    TextView mMyEmailPhone;
    @BindView(R.id.my_binding_email_phone_layout)
    RelativeLayout mMyBindingEmailPhoneLayout;
    @BindView(R.id.my_nick_name)
    TextView mNickName;

    @BindView(R.id.my_img)
    RoundedImageView myHeadIcon;

    @Inject
    MeActivityPresenter mPresenter;


    private Integer[] mHeadIcons = {R.drawable.protrait_1, R.drawable.protrait_2, R.drawable.protrait_3, R.drawable.protrait_4, R.drawable.protrait_5, R.drawable.protrait_6};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_me;
    }

    @Override
    protected void init() {
        DaggerMeActivityPresenterComponent.builder()
                .applicationComponent(((MyApplication) getApplication()).getApplicationComponent())
                .meActivityPresenterModule(new MeActivityPresenterModule(this))
                .build()
                .inject(this);

        if (Utils.myAccountInfo != null) {
            myHeadIcon.setImageResource(Utils.getHeadIconRes(Utils.myAccountInfo.getHead_icon()));
            mMyAccount.setText(Utils.myAccountInfo.getNumber());
            mMyEmailPhone.setText(Utils.myAccountInfo.getEmail());
            mNickName.setText(Utils.myAccountInfo.getNickname());
        }

    }

    @OnClick({R.id.me_back, R.id.my_binding_email_phone_layout, R.id.change_psw, R.id.my_nick_name_layout, R.id.my_photo_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_back:
                finish();
                break;
            case R.id.my_binding_email_phone_layout:
                /*if(!Utils.sUser.isHasbinding()){
                    StartActivityUtils.startRegisterActivity(this, Constant.FROM_ME_BIND);
                }*/
                break;
            case R.id.change_psw:
                StartActivityUtils.startChangePswActivity(MeActivity.this);
                break;

            case R.id.my_nick_name_layout:
                DialogUtils.showOneEditDialog(this, getString(R.string.my_center_set_nick_name), mNickName.getText().toString(), null, getString(R.string.my_center_save), new OnClickOneEdit() {
                    @Override
                    public void onClick(Dialog dialog, String text) {
                        if (StringUtils.isBlank(text)) {
                            ToastUtils.show(MeActivity.this, R.string.nick_name_not_null_tip);
                        } else {
                            ChangeNickNameParam param = new ChangeNickNameParam();
                            param.setNickname(text);
                            mPresenter.changeNickName(Utils.token, param);
                            dialog.dismiss();
                        }
                    }
                });
                break;
            case R.id.my_photo_layout:
                //mPresenter.changeHeadIcon();
                SelectHeadIconDialog.Builder builder = new SelectHeadIconDialog.Builder(this);
                builder.addSingleChoiceItems(mHeadIcons, getString(R.string.select_head_icon_title), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        ChangeHeadIconParam param = new ChangeHeadIconParam("system" + (which + 1));
                        mPresenter.changeHeadIcon(Utils.token, param);
                    }
                });
                builder.show();
                break;
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void dismissProgress() {

    }

    @Override
    public void error(Throwable throwable) {
        ToastUtils.show(this, throwable.getMessage() + "");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.hasExtra(IntentParams.CHANGE_PSW)) {
            mChangePswTip.setVisibility(View.VISIBLE);
            Observable.timer(3000, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
                @Override
                public void call(Long aLong) {
                    mChangePswTip.setVisibility(View.INVISIBLE);
                }
            });
        }
    }

    @Override
    protected void hasGetAllPermissions() {

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.scale_dialog_out);
    }

    @Override
    public void changeNickNameSuccess(String nickName) {
        mNickName.setText(nickName);
    }

    @Override
    public void changeHeadIconSuccess(String headIcon) {
        myHeadIcon.setImageResource(Utils.getHeadIconRes(headIcon));
    }
}
